/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Notificaciones.java
 *
 * Created on 19-10-2009, 11:39:33 PM
 */

package secureplace;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;

//Para enviarCorreo
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.BodyPart;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;

//Para llamar por telefono
import java.io.*;
import java.util.*;
import javax.comm.*;

//Para hacer sonar la alarma audible
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.sound.sampled.*;

/**
 *
 * @author hanscker
 */
public class Notificaciones extends javax.swing.JDialog {

    static boolean llamadaActiva;
    /** Creates new form Notificaciones */
    public Notificaciones(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        completarComboBoxPuertos();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JPasswordField();
        jCheckBox3 = new javax.swing.JCheckBox();
        jCheckBox4 = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        jCheckBox5 = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jTextField8 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(secureplace.SecurePlaceApp.class).getContext().getResourceMap(Notificaciones.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jCheckBox1.setText(resourceMap.getString("jCheckBox1.text")); // NOI18N
        jCheckBox1.setName("jCheckBox1"); // NOI18N
        jCheckBox1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jCheckBox1StateChanged(evt);
            }
        });
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });

        jCheckBox2.setText(resourceMap.getString("jCheckBox2.text")); // NOI18N
        jCheckBox2.setName("jCheckBox2"); // NOI18N

        jTextField2.setText(resourceMap.getString("jTextField2.text")); // NOI18N
        jTextField2.setName("jTextField2"); // NOI18N

        jTextField3.setText(resourceMap.getString("jTextField3.text")); // NOI18N
        jTextField3.setName("jTextField3"); // NOI18N

        jTextField4.setText(resourceMap.getString("jTextField4.text")); // NOI18N
        jTextField4.setName("jTextField4"); // NOI18N

        jTextField5.setText(resourceMap.getString("jTextField5.text")); // NOI18N
        jTextField5.setName("jTextField5"); // NOI18N

        jCheckBox3.setText(resourceMap.getString("jCheckBox3.text")); // NOI18N
        jCheckBox3.setName("jCheckBox3"); // NOI18N

        jCheckBox4.setText(resourceMap.getString("jCheckBox4.text")); // NOI18N
        jCheckBox4.setName("jCheckBox4"); // NOI18N
        jCheckBox4.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jCheckBox4StateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 342, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jCheckBox4)
                    .addComponent(jCheckBox1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jCheckBox2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jCheckBox3)))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBox2)
                    .addComponent(jCheckBox3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTextField1.setText(Parametros.Notificaciones_Email_Direccion);
        if (Parametros.Notificaciones_Email_Autentificacion) {
            jCheckBox1.doClick();
        } else {
            jCheckBox1.doClick();
            jCheckBox1.doClick();
        }
        if (Parametros.Notificaciones_Email_SPA) {jCheckBox2.doClick();}
        jTextField2.setText(Parametros.Notificaciones_Email_Servidor);
        jTextField3.setText(Parametros.Notificaciones_Email_Puerto);
        jTextField4.setText(Parametros.Notificaciones_Email_Usuario);
        jTextField5.setText(Parametros.Notificaciones_Email_Password);
        if (Parametros.Notificaciones_Email_TLS) {jCheckBox3.doClick();}
        jCheckBox4.setText("Enviar desde cuenta secureplace.alert@gmail.com");
        System.out.println("CI"+Parametros.Notificaciones_Email_CuentaInterna);
        if (Parametros.Notificaciones_Email_CuentaInterna) {
            jCheckBox4.doClick();
        } else {
            jCheckBox4.doClick();
            jCheckBox4.doClick();
        }

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel2.border.title"))); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jTextField6.setText(resourceMap.getString("jTextField6.text")); // NOI18N
        jTextField6.setName("jTextField6"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jComboBox1.setName("jComboBox1"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(168, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTextField6.setText(Parametros.Notificaciones_Telefono_Numero);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel3.border.title"))); // NOI18N
        jPanel3.setName("jPanel3"); // NOI18N

        jCheckBox5.setText(resourceMap.getString("jCheckBox5.text")); // NOI18N
        jCheckBox5.setName("jCheckBox5"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jTextField7.setText(resourceMap.getString("jTextField7.text")); // NOI18N
        jTextField7.setName("jTextField7"); // NOI18N

        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText(resourceMap.getString("jButton5.text")); // NOI18N
        jButton5.setName("jButton5"); // NOI18N
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        jTextField8.setText(resourceMap.getString("jTextField8.text")); // NOI18N
        jTextField8.setName("jTextField8"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox5)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField7, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 153, Short.MAX_VALUE)
                        .addComponent(jButton5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jCheckBox5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton4)
                    .addComponent(jButton5)
                    .addComponent(jLabel9)
                    .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        if (Parametros.Notificaciones_Alarma_Activada) {
            jCheckBox5.doClick();
        } else {
            jCheckBox5.doClick();
            jCheckBox5.doClick();
        }
        jTextField7.setText(Parametros.Notificaciones_Alarma_Archivo);
        jTextField8.setText(Parametros.Notificaciones_Alarma_Repeticiones);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    static boolean enviarEmail(String archivo) {

        try {
            System.out.println("Enviando email con archivo: "+archivo);
            SecurePlaceView.EtiquetaEstadoNotificacion.setText("Enviando archivo:"+archivo+" por email");
            Properties conexion = new Properties();
            if (Parametros.Notificaciones_Email_CuentaInterna) {
                conexion.setProperty("mail.smtp.host", "smtp.gmail.com");
                conexion.setProperty("mail.smtp.port","587");
                conexion.setProperty("mail.smtp.user", Parametros.Notificaciones_Email_Direccion);
                conexion.setProperty("mail.smtp.auth", "true");
                conexion.setProperty("mail.smtp.starttls.enable", "false");
                conexion.setProperty("mail.smtp.starttls.enable", "true");
            } else {
                // Nombre del host de correo, es smtp.gmail.com
                conexion.setProperty("mail.smtp.host", Parametros.Notificaciones_Email_Servidor);
                // Puerto de gmail para envio de correos
                conexion.setProperty("mail.smtp.port",Parametros.Notificaciones_Email_Puerto);
                // Nombre del usuario
                conexion.setProperty("mail.smtp.user", Parametros.Notificaciones_Email_Direccion);
                // Si requiere o no usuario y password para conectarse.
                String neaut = new Boolean(Parametros.Notificaciones_Email_Autentificacion).toString();
                conexion.setProperty("mail.smtp.auth", neaut);
                // SPA si está disponible
                String nespa = new Boolean(Parametros.Notificaciones_Email_SPA).toString();
                conexion.setProperty("mail.smtp.starttls.enable", nespa);
                // TLS si está disponible
                String netls = new Boolean(Parametros.Notificaciones_Email_TLS).toString();
                conexion.setProperty("mail.smtp.starttls.enable", netls);
            }
            //Iniciamos la sesion
            Session session = Session.getDefaultInstance(conexion);
            session.setDebug(false);

            // Se compone la parte del texto
            BodyPart texto = new MimeBodyPart();
            texto.setText("Se ha detectado movimiento. Se adjunta el archivo: "+archivo);

            // Se compone el adjunto con la imagen
            BodyPart adjunto = new MimeBodyPart();
            adjunto.setDataHandler(
                new DataHandler(new FileDataSource(archivo)));
            adjunto.setFileName(archivo);

            // Una MultiParte para agrupar texto e imagen.
            MimeMultipart multiParte = new MimeMultipart();
            multiParte.addBodyPart(texto);
            multiParte.addBodyPart(adjunto);

            MimeMessage correo = new MimeMessage(session);
            // Quien envia el e-mail y a quien va dirigido
            if (Parametros.Notificaciones_Email_CuentaInterna) {
                correo.setFrom(new InternetAddress("secureplace.alert@gmail.com"));
            } else {
                correo.setFrom(new InternetAddress(Parametros.Notificaciones_Email_Direccion));
            }
            correo.addRecipient(Message.RecipientType.TO, new InternetAddress(Parametros.Notificaciones_Email_Direccion));
            Transport t = session.getTransport("smtp");
            //Sujeto y adjunto
            correo.setSubject("Alerta de Secure Place");
            correo.setContent(multiParte);

            //Se envia el e-mail
            if (Parametros.Notificaciones_Email_CuentaInterna) {
                t.connect("secureplace.alert@gmail.com","23374309133");
            } else {
                t.connect(Parametros.Notificaciones_Email_Usuario,Parametros.Notificaciones_Email_Password);
            }
            t.sendMessage(correo,correo.getAllRecipients());
            t.close();
            secureplace.SecurePlaceView.EtiquetaEstadoNotificacion.setText("E-mail enviado con archivo: "+archivo);
            System.out.println("E-mail enviado con archivo: "+archivo);
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            secureplace.SecurePlaceView.EtiquetaEstadoNotificacion.setText("Error al enviar correo con archivo:"+archivo);
            System.out.println("Error al enviar correo con archivo: "+archivo);
            return false;
        }
    }

    static class llamarPorTelefono extends Thread {

        CommPortIdentifier idPort;
        CommPortIdentifier modemPort;
        SerialPort sPort;
        OutputStream salida;
        InputStream entrada;

        

        @Override
        public void run() {
            System.out.println("Inicio llamarPorTelefono");
            SecurePlaceView.EtiquetaEstadoNotificacion.setText("Preparando llamada a: "+Parametros.Notificaciones_Telefono_Numero);
            llamadaActiva = true;
            loadModemPort();
        }

        boolean loadModemPort(){

            try {
                modemPort = CommPortIdentifier.getPortIdentifier(Parametros.Notificaciones_Telefono_Puerto);
            } catch (NoSuchPortException e) {System.out.println(e.getMessage());}
            if(modemPort != null){
                try {
                    sPort = (SerialPort)modemPort.open("Secure Place", 10000);
                } catch (PortInUseException e) {
                    System.out.println("El puerto"+Parametros.Notificaciones_Telefono_Puerto+" esta siendo utilizado por "+e.currentOwner);
                    SecurePlaceView.EtiquetaEstadoNotificacion.setText(e.currentOwner + " esta utilizado " + Parametros.Notificaciones_Telefono_Puerto);
                    llamadaActiva = false;
                    return false;
                    //System.out.println(e.getMessage)();
                }
                try {
                    sPort.setSerialPortParams( 9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE );
                } catch (UnsupportedCommOperationException e) {System.out.println(e.getMessage());}
                try {
                    sPort.setFlowControlMode( SerialPort.FLOWCONTROL_NONE );
                } catch (UnsupportedCommOperationException e) {System.out.println(e.getMessage());}
                try {
                    sPort.enableReceiveThreshold( 1 );
                } catch (UnsupportedCommOperationException e) {System.out.println(e.getMessage());}
                try {
                    sPort.enableReceiveTimeout( 10000 );
                } catch (UnsupportedCommOperationException e) {System.out.println(e.getMessage());}
                try {
                    salida = sPort.getOutputStream();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
                String comandoAT = "ATDT,"+Parametros.Notificaciones_Telefono_Numero+";\r\n";
                try {
                    salida.write(comandoAT.getBytes());
                    System.out.println("Llamando a: "+Parametros.Notificaciones_Telefono_Numero);
                    SecurePlaceView.EtiquetaEstadoNotificacion.setText("Llamando a: "+Parametros.Notificaciones_Telefono_Numero);

                    salida.flush();
                    salida.close();
                    sPort.close();
                } catch (IOException e) {
                    System.err.println("Error escritura en el puerto"+sPort.getName());
                    System.out.println(e.getMessage());
                }
                System.out.println("Finalizo la llamada");
                SecurePlaceView.EtiquetaEstadoNotificacion.setText("Inactivo");
            }
            llamadaActiva = false;
            return true;
        }
    }
    
    private String[] cargarListaPuertos() {
            //System.out.println("PortList");
            Enumeration listaPort = null;
            String listaPuertos[] = new String[100];
            int i = 0;
            listaPort = CommPortIdentifier.getPortIdentifiers();
            CommPortIdentifier currentPort = null;
            while(listaPort.hasMoreElements()) {
                currentPort = (CommPortIdentifier)listaPort.nextElement();
                //System.out.println(currentPort.getName()+","+currentPort.getPortType()+","+currentPort.getCurrentOwner());
                listaPuertos[i] = currentPort.getName();
                i++;
            }
            String listaPuertosFiltrada[] = new String[i];
            for (int j=0;j<i;j++) {
                listaPuertosFiltrada[j] = listaPuertos[j];
            }
            return listaPuertosFiltrada;

        }

    static void cargarDriverPuertoSerial() {
            String drivername = "com.sun.comm.Win32Driver";
            CommDriver driver = null;
            try {
                driver = (CommDriver) Class.forName(drivername).newInstance();
            } catch (java.lang.Exception e) {
                System.out.println(e.getMessage());
            }
            driver.initialize();
            System.out.println("Driver de puertos COM y LPT inicializado");
        }

    private void completarComboBoxPuertos() {

            String[] nombresPuertos = null;
            jComboBox1.removeAllItems();
            nombresPuertos = cargarListaPuertos();
            for (int i=0; i<nombresPuertos.length; i++) {
                jComboBox1.addItem(nombresPuertos[i]);
                if (nombresPuertos[i].equals(Parametros.Notificaciones_Telefono_Puerto) ) {
                    //System.out.println(Parametros.Notificaciones_Telefono_Puerto+","+nombresPuertos[i]);
                    jComboBox1.setSelectedItem(Parametros.Notificaciones_Telefono_Puerto);
                }
            }
        }

    
    private void jCheckBox1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jCheckBox1StateChanged
        if (jCheckBox1.isSelected()) {
            jLabel5.setEnabled(true);
            jLabel6.setEnabled(true);
            jTextField4.setEnabled(true);
            jTextField5.setEnabled(true);
            jCheckBox2.setEnabled(true);
            jCheckBox3.setEnabled(true);
        } else {
            jLabel5.setEnabled(false);
            jLabel6.setEnabled(false);
            jTextField4.setEnabled(false);
            jTextField5.setEnabled(false);
            jCheckBox2.setEnabled(false);
            jCheckBox3.setEnabled(false);
        }
    }//GEN-LAST:event_jCheckBox1StateChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Parametros.Notificaciones_Alarma_Activada = jCheckBox5.isSelected();
        Parametros.Notificaciones_Alarma_Archivo = jTextField7.getText();
        Parametros.Notificaciones_Alarma_Repeticiones = jTextField8.getText();
        Parametros.Notificaciones_Email_Direccion = jTextField1.getText();
        Parametros.Notificaciones_Email_CuentaInterna = jCheckBox4.isSelected();
        Parametros.Notificaciones_Email_Servidor = jTextField2.getText();
        Parametros.Notificaciones_Email_Puerto = jTextField3.getText();
        Parametros.Notificaciones_Email_Usuario = jTextField4.getText();
        Parametros.Notificaciones_Email_Password = jTextField5.getText();
        Parametros.Notificaciones_Email_Autentificacion = jCheckBox1.isSelected();
        Parametros.Notificaciones_Email_SPA = jCheckBox2.isSelected();
        Parametros.Notificaciones_Email_TLS = jCheckBox3.isSelected();
        Parametros.Notificaciones_Telefono_Numero = jTextField6.getText();
        Parametros.Notificaciones_Telefono_Puerto = String.valueOf(jComboBox1.getSelectedItem());
        secureplace.Parametros.guardarParametros();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
        if (jCheckBox1.isSelected()) {
            jLabel5.setEnabled(true);
            jLabel6.setEnabled(true);
            jTextField4.setEnabled(true);
            jTextField5.setEnabled(true);
            jCheckBox2.setEnabled(true);
            jCheckBox3.setEnabled(true);
        } else {
            jLabel5.setEnabled(false);
            jLabel6.setEnabled(false);
            jTextField4.setEnabled(false);
            jTextField5.setEnabled(false);
            jCheckBox2.setEnabled(false);
            jCheckBox3.setEnabled(false);
        }
    }//GEN-LAST:event_jCheckBox1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jCheckBox4StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jCheckBox4StateChanged
        if (jCheckBox4.isSelected()) {
            jTextField2.setEnabled(false);
            jTextField3.setEnabled(false);
            jCheckBox1.setEnabled(false);
            jCheckBox1.setSelected(false);
        } else {
            jTextField2.setEnabled(true);
            jTextField3.setEnabled(true);
            jCheckBox1.setEnabled(true);
        }
    }//GEN-LAST:event_jCheckBox4StateChanged

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        File archivo;
        JFileChooser fc = new JFileChooser("C:/Archivos de programa/SecurePlace/sounds");
        fc.addChoosableFileFilter(new FiltroTipoArchivoWav());
        int resp = fc.showOpenDialog(this);

        if(resp == JFileChooser.APPROVE_OPTION){
            archivo = fc.getSelectedFile();
            jTextField7.setText(archivo.getAbsolutePath());
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        if (playClip.clip.isRunning()) {
            playClip.clip.stop();
        }
        
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        if ((!jTextField7.getText().isEmpty()) && (!jTextField8.getText().isEmpty())) {
            Notificaciones.iniciarplayClip(jTextField7.getText(), Integer.parseInt(jTextField8.getText()));
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    class FiltroTipoArchivoWav extends javax.swing.filechooser.FileFilter{
        public boolean accept(File archivo){
            if (archivo.isDirectory()) {
                return true;
            }
            String filename = archivo.getName();
            return filename.endsWith(".wav");
        }
        
        public String getDescription(){
            return "*.wav";
        }
        
    }

    static void iniciarplayClip(String archivo, int repeticiones) {
        new playClip(archivo, repeticiones).start();
    }

    static class playClip extends Thread {
        String archivo;
        int repeticiones;
        static Clip clip;

        public playClip(java.lang.String archivo, int repeticiones) {
            this.archivo = archivo;
            this.repeticiones = repeticiones;
        }

        @Override
        public void run() {
            System.out.println("PlayClip "+archivo+" "+repeticiones);
            Audio.stopCapture = true;
            try {
                File soundFile = new File(archivo);
                AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
                clip = AudioSystem.getClip();
                System.out.println("Clip.open");
                clip.open(audioIn);
                System.out.println("Clip.loop");
                clip.loop(repeticiones-1);
                try {
                    int duracion = java.lang.Math.round(clip.getMicrosecondLength()/1000);
                    System.out.println(duracion);
                    Thread.sleep(duracion*repeticiones);
                }catch(InterruptedException e) {
                    e.printStackTrace();
                }
                clip.close();
                if (secureplace.SecurePlaceView.EtiquetaModoActual.getText().equalsIgnoreCase("Modo Vigilancia")) {
                    Audio.inicio();
                }
            } catch (UnsupportedAudioFileException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (LineUnavailableException e) {
                e.printStackTrace();
            }
        }

    }
        

    

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Notificaciones dialog = new Notificaciones(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JCheckBox jCheckBox4;
    private javax.swing.JCheckBox jCheckBox5;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    // End of variables declaration//GEN-END:variables

}
